package View;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JInternalFrame;

import Controller.ABMMineralController;
import Model.ObjectViews.MineralView;

import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ABMMineralView extends JInternalFrame implements MouseListener, ActionListener 
{
	private static final long serialVersionUID = 1L;

	enum ButtonActions {Guardar, Cancelar, Eliminar};
	
	private ABMMineralController controller;
	private JScrollPane mineralesGridPanel;
	private JPanel panelEdicion;
	private JTable mineralesTableView;
	private JTextField txtCodigo;
	private JTextField txtNombre;
	private JButton btnGuardar;
	private JButton btnCancelar;
	private JButton btnEliminar;
	
	/**
	 * Create the frame.
	 */
	public ABMMineralView(String titulo, String action) 
	{
		setBounds(100, 100, 750, 550);
		this.setTitle(titulo);
		getContentPane().setLayout(null);
		
		mineralesGridPanel = new JScrollPane();
		mineralesGridPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		mineralesGridPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		mineralesGridPanel.setBounds(10, 10, 720, 115);
		getContentPane().add(mineralesGridPanel);
		
		panelEdicion = new JPanel();
		panelEdicion.setBounds(10, 136, 720, 199);
		getContentPane().add(panelEdicion);
		panelEdicion.setLayout(null);
		
		JLabel lblCodigo = new JLabel("Codigo:");
		lblCodigo.setBounds(10, 11, 80, 14);
		panelEdicion.add(lblCodigo);
		
		txtCodigo = new JTextField();
		txtCodigo.setBounds(100, 8, 337, 20);
		panelEdicion.add(txtCodigo);
		txtCodigo.setColumns(10);
		
		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setBounds(10, 39, 80, 14);
		panelEdicion.add(lblNombre);
		
		txtNombre = new JTextField();
		txtNombre.setBounds(100, 36, 337, 20);
		panelEdicion.add(txtNombre);
		txtNombre.setColumns(10);
		
		btnGuardar = new JButton("Guardar");
		btnGuardar.addActionListener(this);
		btnGuardar.setBounds(95, 83, 89, 23);
		panelEdicion.add(btnGuardar);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(this);
		btnCancelar.setBounds(225, 83, 89, 23);
		panelEdicion.add(btnCancelar);
		
		btnEliminar = new JButton("Eliminar");
		btnEliminar.addActionListener(this);
		btnEliminar.setBounds(348, 83, 89, 23);
		panelEdicion.add(btnEliminar);
		
		//Seteo el controlador
		controller = new ABMMineralController(action, this);
		controller.init();
	}
	
	public void setMineralesGrid(ArrayList<MineralView> minerales)
	{
		NoneEditableCellTableModel model = new NoneEditableCellTableModel();
		model.addColumn("Codigo");
		model.addColumn("Nombre");
		mineralesTableView = new JTable(model);
		mineralesTableView.addMouseListener(this);
		
		for (Iterator<MineralView> iterator = minerales.iterator(); iterator.hasNext();) 
		{
			MineralView mineral = (MineralView) iterator.next();
			model.addRow(new Object[]{mineral.getCodigo(), mineral.getNombre()});
		}
		
		mineralesGridPanel.setViewportView(mineralesTableView);
	}
	
	public void showGrillaDeMinerales(boolean show)
	{
		mineralesGridPanel.setVisible(show);
	}
	
	public void showPanelDeEdicion(boolean show)
	{
		panelEdicion.setVisible(show);
	}
	
	public void showBotonGuardar(boolean show)
	{
		this.btnGuardar.setVisible(show);
	}
	
	public void showBotonCancelar(boolean show)
	{
		this.btnCancelar.setVisible(show);
	}
	
	public void showBotonEliminar(boolean show)
	{
		this.btnEliminar.setVisible(show);
	}
	
	public void setItemData(MineralView mineral)
	{
		txtCodigo.setText(mineral.getCodigo());
		txtNombre.setText(mineral.getNombre());
	}
	
	public MineralView getItemData()
	{
		MineralView mineralView = new MineralView();
		mineralView.setCodigo(txtCodigo.getText());
		mineralView.setNombre(txtNombre.getText());
		
		return mineralView;
	}
	
	public void mouseClicked(MouseEvent e)
	{
		int fila = mineralesTableView.rowAtPoint(e.getPoint());
		if (fila > -1)
		{
			controller.seleccionarMineral(fila);
		}
	}

	//Actions handlers
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void actionPerformed(ActionEvent e) 
	{
		ButtonActions action = ButtonActions.valueOf(e.getActionCommand());
		
		switch (action) {
		case Guardar:
			controller.Guardar();
			break;
		case Cancelar:
			controller.Cancelar();
			break;
		case Eliminar:
			controller.Eliminar();
			break;

		default:
			break;
		}
		
	}
}
